package com.lin.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lin.reggie.common.CustomException;
import com.lin.reggie.entity.Category;
import com.lin.reggie.entity.Dish;
import com.lin.reggie.entity.Setmeal;
import com.lin.reggie.mapper.CategoryMapper;
import com.lin.reggie.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.reggie.service.DishService;
import com.lin.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.callback.LanguageCallback;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author lin
 * @since 2022-05-26
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;


    //根据id删除分类，删除之前需要进行判断
    @Override
    public void removeCategory(Long id) {
        //构造条件构造器
        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id进行查询
        dishQueryWrapper.eq(Dish::getCategoryId, id);

        int count2 = dishService.count(dishQueryWrapper);

        //查询当前分类是否关联了菜品，如果已经关联，抛出一个业务异常
        if (count2>0){
            //证明已经关联了菜品，抛出异常
            throw new CustomException("当前分类关联了菜品不能删除");
        }

        //查询当前分类是否关联了套餐，如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setMealquerywrapper = new LambdaQueryWrapper<>();
        setMealquerywrapper.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(setMealquerywrapper);

        if (count1>0){
            //证明已经关联了套餐，抛出异常
            throw new CustomException("当前分类已经关联了套餐不能删除");
        }

        //正常删除
        super.removeById(id);
    }


}
